package com.ruoyi.system.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.utils.CommonStreamUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.web.page.TableSupport;
import com.ruoyi.common.core.web.page.WebTableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.BaseBasketballPlayer;
import com.ruoyi.system.domain.BaseBasketballTeam;
import com.ruoyi.system.domain.BaseContest;
import com.ruoyi.system.domain.dto.Web.BasketBallPlayerDto;
import com.ruoyi.system.domain.dto.Web.BasketBallPlayerExcel;
import com.ruoyi.system.domain.dto.Web.PlayerMoveDto;
import com.ruoyi.system.service.IBaseBasketballPlayerService;
import com.ruoyi.system.service.IBaseBasketballTeamService;
import com.ruoyi.system.service.IBaseContestService;
import com.ruoyi.system.service.impl.LoginUserSetUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.hutool.core.date.DateTime.now;

/**
 * web球员列表
 * 
 * @author wangbj
 * @date 2024-04-16
 */
@ApiOperation("web球员列表")
@RestController
@RequestMapping("/web/basketballPlayer")
public class BaseWebBasketballPlayerController extends BaseController
{
    @Autowired
    private IBaseBasketballPlayerService baseBasketballPlayerService;
    @Autowired
    private IBaseContestService baseContestService;
    @Autowired
    private IBaseBasketballTeamService baseBasketballTeamService;
    @Resource
    LoginUserSetUtil loginUserSetUtil;
    /**
     * 查询球队列表
     */
    @GetMapping("/playerTeamList")
    @Operation(summary = "查询球队列表", description = "查询球队列表")
    public WebTableDataInfo playerTeamList(Long contestId,String teamName)
    {
        startPage();
        List<BaseBasketballTeam> list = baseBasketballPlayerService.playerTeamList(contestId,teamName);
        return getWebDataTable(list);
    }

    /**
     * 新增球队
     */
    @Log(title = "新增球队", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@Validated @RequestBody BaseBasketballTeam baseBasketballTeam)
    {
        List<BaseBasketballTeam> teamList = baseBasketballTeamService.selectByTeamName(baseBasketballTeam.getLeagueId(), Collections.singleton(baseBasketballTeam.getTeamName()));
        if (CollUtil.isNotEmpty(teamList)){
            if (Objects.isNull(baseBasketballTeam.getId())){
                throw new RuntimeException("球队名称已存在!");
            }else{
                if (!Objects.equals(teamList.get(0).getId(), baseBasketballTeam.getId())){
                    throw new RuntimeException("球队名称已存在!");
                }
            }
        }
        baseBasketballTeam.setCreateUserId(SecurityUtils.getUserId());
        baseBasketballTeam.setCreateUserName(SecurityUtils.getUsername());
        baseBasketballTeam.setCreateTime(now());
        if (Objects.nonNull(baseBasketballTeam.getId())) {
            baseBasketballTeam.setUpdateUserName(SecurityUtils.getUsername());
            baseBasketballTeam.setUpdateUserId(SecurityUtils.getUserId());
            baseBasketballTeam.setUpdateTime(now());
        }
        return success(baseBasketballTeamService.saveOrUpdate(baseBasketballTeam));
    }

    /**
     * 删除球队
     */
    @Log(title = "删除球队", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody BaseBasketballTeam baseBasketballTeam)
    {
        loginUserSetUtil.populateFields(baseBasketballTeam, 2);
        baseBasketballTeam.setIsDeleted(1L);
        return success(baseBasketballTeamService.updateById(baseBasketballTeam));
    }


    /**
     * 查询球员列表
     */
    @GetMapping("/playerList")
    @Operation(summary = "查询球员列表", description = "查询球员列表")
    public WebTableDataInfo playerList(BasketBallPlayerDto basketBallPlayerDto)
    {
        List<BaseContest> baseContests = baseContestService.selectBaseContestList(new BaseContest());
        if (CollUtil.isEmpty(baseContests)){
            return getWebDataTable(new ArrayList<>());
        }
        basketBallPlayerDto.setContestIds(CommonStreamUtil.transList(baseContests, BaseContest::getId));
        startPage();
        List<BaseBasketballPlayer> list = baseBasketballPlayerService.playerList(basketBallPlayerDto);
        return getWebDataTable(list);
    }

    /**
     * importPlayer
     * @param file
     * @param contestId
     * @param teamId
     * @return
     * @throws Exception
     */
    @Log(title = "导入球员", businessType = BusinessType.IMPORT)
    @PostMapping("/importPlayer")
    public AjaxResult importPlayer(MultipartFile file,Long contestId) throws Exception
    {
        AssertUtil.notNull(contestId,"联赛不能为空");

        ExcelUtil<BasketBallPlayerExcel> util = new ExcelUtil<BasketBallPlayerExcel>(BasketBallPlayerExcel.class);
        List<BasketBallPlayerExcel> playerList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = baseBasketballPlayerService.importPlayer(playerList,  operName,contestId);
        return success(message);
    }

    /**
     * 迁移球员
     */
    @Log(title = "迁移球员", businessType = BusinessType.UPDATE)
    @PostMapping("/movePlayer")
    public AjaxResult movePlayer(@RequestBody PlayerMoveDto playerMoveDto){
        return success(baseBasketballPlayerService.movePlayer(playerMoveDto));
    }
}
